package com.hhl.flower.controller;

import com.hhl.flower.common.result.Result;
import com.hhl.flower.pojo.vo.FlowerVO;
import com.hhl.flower.service.FlowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/flower")
@Api(tags = "花卉相关接口")
@Slf4j
public class FlowerController {

	@Autowired
	private FlowerService flowerService;

	@GetMapping("/list")
	@ApiOperation("查询所有花卉")
    public Result<List<FlowerVO>> list(){
		List<FlowerVO> fowerList = flowerService.getFowerList();

		return Result.success(fowerList);
    }

	@GetMapping("/getInfo")
	@ApiOperation("根据ID查询花卉信息")
	public Result<FlowerVO> getById(@RequestParam("fid") Integer fid){
		log.info("查询花卉信息:{}", fid);
		FlowerVO flower = flowerService.getByFlowerId(fid);

		return Result.success(flower);
	}

	@GetMapping("/getFlowerByNameLike")
	@ApiOperation("根据名称模糊查询花卉")
	public Result<List<FlowerVO>> getByNameLike(@RequestParam(value = "keyword", required = false) String keyword){
		List<FlowerVO> flowerList;
		if (keyword == null || keyword.isEmpty()) {
			flowerList = flowerService.getFowerList();
		} else {
			flowerList = flowerService.getByNameLike(keyword);
		}

		return Result.success(flowerList);
	}

	@GetMapping("/getFlowerByType")
    @ApiOperation("根据分类查询花卉")
    public Result<List<FlowerVO>> getByType(@RequestParam("tid") Integer tid){
        List<FlowerVO> fowerList = flowerService.getByType(tid);

        return Result.success(fowerList);
    }

	@PostMapping("/view")
	@ApiOperation("增加浏览量")
	public Result addViews(@RequestParam("fid") Integer fid){
		flowerService.addViews(fid);

		return Result.success();
	}


}
